<?php
if(isset($_POST['action']))
{
    $db = new Database;
    // User Info
        $auth = Auth::getAuth('current_user');
        $user_id = $auth['id'];
        $company_id =  $auth['company_id'];
        $user_group = $auth['role'];
        $user_groupType = $auth['role_type'];
        $userPositionID = $auth['position_id'];
        $getPosition = $db->query("SELECT * FROM tbposition where id = '$userPositionID'","row");
        
    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 20; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    
    // For Sorting
        $dataSortValue = $_POST['dataSortValue'];
        if(!empty($dataSortValue)){
            if($_POST['counterSort']%2==0){
                $sort = "ORDER BY reasonForRevision DESC";
            }else{
                $sort = "ORDER BY reasonForRevision ASC";
            }
        }else{
            $sort = "ORDER BY reasonForRevision";
        }
    if($action=="RevisionList")
    {
        $count = $db->query("SELECT * FROM tborgchart where company_id = '$company_id' and is_active = 1","numrows");
        $queryRequest = $db->query("SELECT * FROM tborgchart where company_id = '$company_id' and is_active = 1 $sort LIMIT $start, $per_page","array");
        $no_of_paginations = ceil($count / $per_page);
        
    }elseif($_POST['action']=="search_request"){
        $value = $_POST['value'];
        $count = $db->query("SELECT * FROM tborgchart where company_id = '$company_id' AND reasonForRevision LIKE '%$value%' and is_active = 1","numrows");
        $queryRequest = $db->query("SELECT * FROM tborgchart where company_id = '$company_id' AND reasonForRevision LIKE '%$value%' and is_active = 1 $sort LIMIT $start, $per_page","array");
        $no_of_paginations = ceil($count / $per_page);
    }elseif($_POST['action']=="searchDate"){
        $value = $_POST['value'];
        $split = explode(",", $value);
        $count = $db->query("SELECT * FROM tbrequest r
                                    LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                    LEFT JOIN tbform f on f.id=r.form_id
                                    LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                    WHERE (".roleApprover($user_groupType,$user_group)."
                                    or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                    and rp.status=0 and r.company_id = $company_id
                                    and r.status=0 AND r.date>='$split[0]' AND r.date<='$split[1]'","numrows");
        
        
        $queryRequest = $db->query("SELECT sf.numCol as numCol, sf.colValue_formID as colValue_formID,
                                   r.date as date_request,r.user_id as user_id, f.id as form_id,
                                rp.wf_master_id as wf_master_id, f.form_title as form_title, r.data as data,
                                r.id as request_id,rp.id as requestProcess_id FROM tbrequest r
                                LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                LEFT JOIN tbform f on f.id=r.form_id
                                LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                WHERE  (".roleApprover($user_groupType,$user_group)."
                                or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                AND rp.status=0 and r.status=0 AND r.date>='$split[0]' AND r.date<='$split[1]' and r.company_id = $company_id LIMIT $start, $per_page","array");
        $no_of_paginations = ceil($count / $per_page);
    }
    $finaldata.= '<table class="table_data">
                        <thead>
                            <tr id="sortRequestApprove" data-location="tblRequestToApprove" data-search="form_title" data-tag-action="RequestApprove" p="'.$page.'" data-element="#requestToApprove_tbl">
                                <th>#</th>
                                <th>Status</th>
                                <th>Reason for revision</th>
                                <th colspan="3">Actions</th>';
    $finaldata.= '          </tr>
                        </thead>
                        <tbody class="requestToApproveBody">';
    $counter = 1;
    if($count>0){
        foreach($queryRequest as $dataRequest){
            $finaldata.='
                <tr class="diagram_rows '.activeDiagram($dataRequest['status']).' revisiontList_tbl" id="tr_'.$dataRequest['id'].'" rel="'.$dataRequest['id'].'" title="Diagram Name: '.$dataRequest['title'].'">
                    <td width="30"><center>'.$counter++.'</center></td>
                    <td>'.status($dataRequest['status']).'</td>
                    <td>'.$dataRequest['reasonForRevision'].'</td>
                    '.statCol($dataRequest['status'],$dataRequest['id'],"diagram").'
                </tr>';
	}
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$value);
    }else{
        $finaldata.='<tr><td colspan="5"><center>No Record Found</center></td></tr>';
        echo $finaldata;
    }
    
}
?>
<script>
    $(".actionUserRequest").tooltip();
</script>
